﻿var async = false;
var isAdd = true;
var NotValidateInputs = new Array();
var HTMLInputTypes = new Array("input", "textarea", "select");

var reg_EMail = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
var reg_QQ = /[1-9][0-9]{4,}/;
var reg_Post = /[1-9]\d{5}(?!\d)/;
var reg_IDCard = /\d{15}|\d{18}/;
var reg_UID = /^[a-zA-Z][a-zA-Z0-9_]{2,15}$/;
var reg_CN = /[\u4e00-\u9fa5]/;
var reg_Tel = /^\d{3,4}-\d{7,8}(-\d{3,4})?$/;
//var reg_Mobile = /^((\(\d{3}\))|(\d{3}\-))?13[0-9]\d{8}|15[89]\d{8}/;
var reg_Mobile = /^(13[0-9]|15[0|1|2|3|5|6|7|8|9]|18[0|3|5|6|7|8|9]|147)\d{8}$/;
var reg_Number = /^-?[0-9]\d*$/;
var reg_URI = "([/w-]+/.)+[/w-]+.([^a-z])(/[/w-: ./?%&=]*)?|[a-zA-Z/-/.][/w-]+.([^a-z])(/[/w-: ./?%&=]*)?";
var reg_Decimal = /^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$/;
var reg_Date = /((^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(10|12|0?[13578])([-\/\._])(3[01]|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(11|0?[469])([-\/\._])(30|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(0?2)([-\/\._])(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)([-\/\._])(0?2)([-\/\._])(29)$)|(^([3579][26]00)([-\/\._])(0?2)([-\/\._])(29)$)|(^([1][89][0][48])([-\/\._])(0?2)([-\/\._])(29)$)|(^([2-9][0-9][0][48])([-\/\._])(0?2)([-\/\._])(29)$)|(^([1][89][2468][048])([-\/\._])(0?2)([-\/\._])(29)$)|(^([2-9][0-9][2468][048])([-\/\._])(0?2)([-\/\._])(29)$)|(^([1][89][13579][26])([-\/\._])(0?2)([-\/\._])(29)$)|(^([2-9][0-9][13579][26])([-\/\._])(0?2)([-\/\._])(29)$))/;
var reg_LongDate = /^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$/; 
var reg_Image = /.+\.[J|j][P|p][G|g]$|.+\.[J|j][P|p][E|e][G|g]$|.+\.[G|g][I|i][F|f]$|.+\.[P|p][N|n][G|g]$|.+\.[B|b][M|m][P|p]$/;
var reg_CardID = /^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/;

function ValidateByID(id) {
    return Validate($("#" + id));
}

function Validate(cb) {
    if (typeof OnValidating != 'undefined')
        OnValidating(cb);
    var emp = cb.attr("emp"); //设置则不可为空！
    var unq = cb.attr("unq"); //设置则必须唯一
    var exp = cb.attr("exp"); //正则表达式
    var id = cb.attr("id");
    var des = cb.attr("des"); //描述
    var name = cb.attr("name");
    var errorID = cb.attr("errorID");
    var disabled = cb.attr("disabled");
    var oldData = cb.attr("olddata");
    var cbType = cb.attr("type");
    var val = cb.val();

    var err = null;
    var errorIDS = errorID;

    if (CheckNotNull(id)) {
        if (typeof errorIDS == 'undefined' || errorIDS == null || errorIDS == "") {
            if (typeof name != 'undefined' && name.indexOf("$") >= 0) { //兼容 net 3.5
                var tempIDS = name.split("$");
                errorIDS = "error" + tempIDS[tempIDS.length - 1];
            }
            else
                errorIDS = "error" + id;
        }
    }

    if (CheckNull(errorID)) {
        if (cbType != "hidden" && cbType != "button" && cbType != "image" && cbType != "submit") {
            $("#" + errorIDS).remove();
            err = $("<p id='" + errorIDS + "'></p>");
            $(cb).after(err);
        }
        else
            return true;
    }
    else {
        err = $("#" + errorID);
    }

    if (CheckNull(val) && CheckNotNull(emp) && CheckNotNull(err) && CheckNotNull(des)) {
        err.html((des + '不能为空').ToError());
        if (!async)
            cb.focus();
        return false;
    }
    if (typeof ValidateExtend != 'undefined') {
        var extendRsult = ValidateExtend(cb, id, err);
        if (!extendRsult)
            return false;
    }
    if (CheckNotNull(val) && CheckNotNull(exp)) {
        var reg;
        if (exp.indexOf("reg_") >= 0) {
            reg = new RegExp(eval(exp));
        }
        else {
            if (exp.indexOf("Compare_") >= 0) {
                var cid = exp.replace('Compare_', '');
                if (val != $("#" + cid).val()) {
                    if (CheckNotNull(err) && CheckNotNull(des)) {
                        err.html((des + '不正确,两次输入不一致.').ToError());
                    }
                    if (!async)
                        cb.focus();
                    return false;
                }
                if (CheckNotNull(err)) {
                    if (CheckNull(errorID))
                        err.remove();
                    else
                        err.html('');
                    return true;
                }
            }
            else {
                reg = new RegExp(exp);
            }
        }
        if (!reg.test(val) && CheckNotNull(err) && CheckNotNull(des)) {
            err.html((des + '不正确').ToError());
            if (!async)
                cb.focus();
            return false;
        }
    }

    if (CheckIsAdd(disabled) && CheckNotNull(unq)) {
        if (oldData != null && oldData != '') {
            if (oldData == val) {
                if (CheckNull(errorID))
                    err.remove();
                else
                    err.html('');
                return true;
            }
        }
        var r = CheckField(id, name, val, function (re) {
            var res = CheckUnQ(re, err, val);
            if (!res && !async)
                cb.focus();
            if (res) {
                if (CheckNull(errorID))
                    err.remove();
                else
                    err.html('');
            }
            return res;
        });
        var resu = CheckUnQ(r, err, val);
        if (!resu && !async)
            cb.focus();
        if (resu) {
            if (CheckNull(errorID))
                err.remove();
            else
                err.html('');
        }
        return resu;

    }
    if (CheckNotNull(err)) {
        if (CheckNull(errorID))
            err.remove();
        else
            err.html('');
        return true;
    }
}

function CheckIsAdd(disabled) {
    if (isAdd && CheckNull(disabled)) {
        return true;
    }
    else
        return false;
}

function CheckUnQ(re, err, val) {
    if (re && typeof err != 'undefined') {
        err.html((val + '已经存在.').ToError());
        return false;
    }
    else {
        err.html('');
        return true;
    }
}

function CheckField(id, name, value, callBack) {
    if (value == '') {
        callBack(false);
        return false;
    }
    var result = false;
    var postData = "{id:'" + id + "',name:'" + name + "',value:'" + value + "'}";
    var url = vRoot + "Services/WebService.asmx/CheckField";
    $.ajax({
        url: url,
        type: "POST",
        data: postData,
        contentType: "application/json; charset=UTF-8",
        dataType: "json",
        async: async,
        success: function (data) {
            result = data.d;
            if (async)
                callBack(data.d);
        },
        error: function (f) {
            result = false;
        }
    });
    return result;
}
function ValidateInContainer(containerId, notValidateCallBack, iFCheckNotValidateInputs) {
    var result = true;
    for (var input in HTMLInputTypes) {
        if (typeof HTMLInputTypes[input] != 'function' && result)
            result = ValidateInputsInContainerByMark(containerId, HTMLInputTypes[input], notValidateCallBack, iFCheckNotValidateInputs);
    }
    return result;
}

function ValidateInputsInContainerByMark(containerId, markName, notValidateCallBack, iFCheckNotValidateInputs) {
    var result = true;
    $("#" + containerId).find(markName).each(function () {
        var inputID = $(this).attr("id");
        if (CheckNotNull(inputID) && (iFCheckNotValidateInputs || CheckNull(NotValidateInputs[inputID]))) {
            if (!Validate($(this))) {
                if (typeof notValidateCallBack != 'undefined') {
                    notValidateCallBack();
                }
                result = false;
                return false;
            }
        }
    });
    return result;
}